<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

/**
 * 银行卡总表
 */
class UserBank extends CxhshopBase
{

    protected $table = 'ims_cxh_user_bank';

    protected $fillable = ['member_id', 'b_id', 'bank_no', 'user_name', 'address', 'ctime','member_uuid'];

    public $timestamps = false;

    const CREATED_AT = 'ctime';

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];

    /**
     * 根据用户id获取银行卡列表
     * @param $uuid   member表里的member_uuid
     * @return array
     */
    public function getBankListById($uuid)
    {
        $query = self::query();
        $list = $query->where("member_uuid", $uuid)->where('is_del', 0) -> orderBy('id','desc')->get(['id', 'b_id', 'bank_no', 'user_name', 'address']);
        if($list){
            $bankList = $list -> toArray();
            foreach ($bankList as $key => $value) {
                $bank = new Bank();
                $bankInfo = $bank->getBankInfoById($value['b_id']);
                $bankList[$key]['bank_name'] = $bankInfo['bank'];
                $bankList[$key]['background_url'] = $bankInfo['background_url'];
                $bankList[$key]['logo_url'] = $bankInfo['logo_url'];
            }
        }else{
            $bankList = [];
        }
        return $bankList;
    }

    /**
     * 添加银行卡
     * @param $uuid
     * @param $memId
     * @param $params 所需参数
     * @return mixed
     */
    public function bankAdd($uuid,$memId, $params)
    {
        $bId = $params['bid']; //所属银行id
        $userName = $params['username'];
        $address = $params['address']; //开户行地址
        $bankNo = $params['bankno']; //银行卡号
        $ctime = time(); //银行卡号
        $data = [
            'member_id' => $memId,
            'member_uuid' => $uuid,
            'b_id' => $bId,
            'bank_no' => $bankNo,
            'user_name' => $userName,
            'address' => $address,
            'ctime' => $ctime,
        ];
        $res = self::create($data);
        return $res;
    }
    /**
     * 获取银行卡详情
     */
    public function getUserBankDetailById($id)
    {
        $query = self::query();
        $bankInfo = $query->where("id", $id)->first(['id', 'b_id', 'bank_no', 'user_name', 'address']);
        //用查询到的结果 再去取需要查询的字段；
        $name = $bankInfo -> getBankInfo -> bank;
        if ($name) {
            $bankInfo -> bank_name = $name;
            $bankInfo = $bankInfo -> toArray();
            unset($bankInfo['get_bank_info']);
        }
        return $bankInfo;
    }
    /**
     *一对一
     *方法名为需要查询的字段
     */
    public function getBankInfo()
    {
        return $this->hasOne('App\Models\Cxhshop\Bank', 'id');
    }

    /**
     * @param $id 删除的银行卡id
     * @param $uuid 用户uuid
     * @return int
     * 删除银行卡
     */
    public function delBankById($id,$uuid){
        $query = self::query();
        $saveData = ['is_del'=>1];
        $res = $query -> where('id',$id) ->where('member_uuid',$uuid) -> update($saveData);
        return $res;
    }
    /**
     * @param $bankNo
     * @return int
     * 查询添加的银行卡是否存在
     */
    public function isRepeatBankNo($bankNo){
        $query = self::query();
        $num = $query -> where('bank_no',$bankNo) -> count();
        return $num;
    }
}
